package android_serialport_api;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.InvalidParameterException;

import android.content.Context;
import android.util.Log;

public class SerialPortCommunications {

	public SerialPort MySerialPort = null;
	public OutputStream MyOutputStream = null;
	public InputStream MyInputStream = null;
	String path = "dev/s3c2410_serial1";
	int baudrate = 57600;

	public SerialPortCommunications(Context context, String databaseName) {
		GetSerialPort();
		try {
			MyOutputStream = MySerialPort.getOutputStream();
			MyInputStream = MySerialPort.getInputStream();
		} catch (Exception e) {
			// TODO: handle exception
		}

	}

	/*
	 * ��ȡ����+0
	 */
	public void GetSerialPort() {
		if (MySerialPort == null) {

			if ((path.length() == 0) || (baudrate == -1)) {
				throw new InvalidParameterException();
			}

			/* Open the serial port */
			try {
				File f = new File(path);
				try {
					MySerialPort = new SerialPort(f, baudrate, 0);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} catch (SecurityException e) {
				// TODO Auto-generated catch block
				Log.e("test SerialPort GetSerialPort",
						"=============================" + e.toString());
				e.printStackTrace();
			}
		}
		// sqlite.MySQLiyeDatabase.close();
	}

	public void SendDate(String Msg) throws IOException {
		try {
			String msg = "";

			if (Msg != null) {
				msg = Msg.replaceAll(" ", "");
			} else {
				return;
			}
			Log.e("TestCommad", msg);
			MyOutputStream.write(hexStringToByte(msg.trim()));
			// MyOutputStream.write('\n');
			MyOutputStream.flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			Log.e("test SerialPort SendDate", "============================="
					+ e.toString());
			e.printStackTrace();
			SerialPortClose();
		}
	}

	byte[] bb = new byte[1024];
	int readtemp = 0;

	public String GetMSg() {

		try {
			if ((readtemp = MyInputStream.read(bb, 0, 1024)) > 0) {
				return bytesToHexString(bb, readtemp);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block

			Log.e("test SerialPort GetMSg",
					"=============================" + e.toString());
			e.printStackTrace();
		}
		return "";
	}

	/*
	 * �رմ���
	 */
	public void SerialPortClose() {
		if (MySerialPort != null) {
			MySerialPort.close();
			MySerialPort = null;
		}
	}

	/**
	 * ��16�����ַ���ת�����ֽ�����
	 * 
	 * @param hex
	 * @return
	 */
	public static byte[] hexStringToByte(String hex) {
		int len = (hex.length() / 2);
		byte[] result = new byte[len];
		char[] achar = hex.toCharArray();
		for (int i = 0; i < len; i++) {
			int pos = i * 2;
			result[i] = (byte) (toByte(achar[pos]) << 4 | toByte(achar[pos + 1]));
		}
		return result;
	}

	private static byte toByte(char c) {
		byte b = (byte) "0123456789ABCDEF".indexOf(c);
		return b;
	}

	/**
	 * ���ֽ�����ת����16�����ַ���
	 * 
	 * @param bArray
	 * @return
	 */
	public static final String bytesToHexString(byte[] bArray, int num) {
		StringBuffer sb = new StringBuffer(bArray.length);
		String sTemp;
		for (int i = 0; i < num; i++) {
			sTemp = Integer.toHexString(0xFF & bArray[i]);
			if (sTemp.length() < 2)
				sb.append(0);
			sb.append(sTemp.toUpperCase());
		}
		return sb.toString();
	}

}
